package com.subl.service.impl;

import com.subl.mapper.UserMapper;
import com.subl.mapper2.UserMapper2;
import com.subl.pojo.User;
import com.subl.service.UserService;
import com.subl.mybatis.annotation.GlobalTransaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserMapper2 userMapper2;

    @GlobalTransaction
    public String doTransUser() {
        List<User> userList = userMapper.selectList(null);
        userMapper2.delete(null);
        userList.forEach(u->userMapper2.insert(u));
        // 异常测试
        //int i = 1/0;
        return "OK";
    }

    //@Transactional
    @GlobalTransaction
    public String updateUser() {
        User user = userMapper.selectById(1);
        Random random = new Random();
        int age = random.nextInt(100);
        log.info("生成的年龄为{}", age);
        user.setAge(age);
        userMapper.updateById(user);
        int a = 1/0;
        return "age:" + age;
    }

    @Override
    public List<User> getUserList() {
        return userMapper.selectList(null);
    }
}
